#!/usr/bin/python

import sys
try:
	import pygtk
	pygtk.require("2.0")
except:
	pass
try:
	import gtk
	import gtk.glade
except:
	sys.exit(1)

#Set the Glade file
gladefile = "qmwindow.glade"

class appWindow:
	def __init__(self):
		self.widgetTree = gtk.glade.XML(gladefile)

		#Get the About Dialog
		self.about = self.widgetTree.get_widget("aboutdialog1")

		#Get the About addQuoteDialog
		self.addquote = self.widgetTree.get_widget("addQuoteDialog")
		self.quoteText = self.widgetTree.get_widget("quoteText")
		self.author = self.widgetTree.get_widget("author")
		self.origin = self.widgetTree.get_widget("origin")
		self.tagsText = self.widgetTree.get_widget("tagsText")
		self.quote = gtk.TextBuffer(None)
		self.tags = gtk.TextBuffer(None)
		self.quoteText.set_buffer(self.quote)
		self.tagsText.set_buffer(self.tags)
	
		#TreeView
		self.selectionsTreeview = self.widgetTree.get_widget("selectionsTreeview")
		self.selectionStore = gtk.ListStore(str)
		self.selectionsTreeview.set_model(self.selectionStore)
		column = gtk.TreeViewColumn('Selecoes', gtk.CellRendererText(), text=0)
		self.selectionsTreeview.append_column(column)

		#TreeView
		self.quoteTreeview = self.widgetTree.get_widget("quoteTreeview")
		self.quoteStore = gtk.ListStore(str, str, str, str)
		column1 = gtk.TreeViewColumn('Citacao', gtk.CellRendererText(), text=0)
		column1.set_min_width(400)
		column2 = gtk.TreeViewColumn('Autor', gtk.CellRendererText(), text=1)
		self.quoteTreeview.append_column(column1)
		self.quoteTreeview.append_column(column2)
		self.quoteFilter = self.quoteStore.filter_new()
		self.quoteTreeview.set_model(self.quoteFilter)
		self.quoteFilter.set_visible_func(self.quotesVisible, data=None)

		#Get the Main Window, and connect the "delete-event" event
		self.window = self.widgetTree.get_widget("qmwindow")
		self.window.connect("delete-event", gtk.main_quit)
		#Widgets
		self.addQuoteButton = self.widgetTree.get_widget("addQuoteButton")
		self.removeQuoteButton = self.widgetTree.get_widget("removeQuoteButton")
		self.hasTextEntry = self.widgetTree.get_widget("hasTextEntry")
		self.hasTagsEntry = self.widgetTree.get_widget("hasTagsEntry")
		self.authorCombo = self.widgetTree.get_widget("authorCombo")
		self.authorList = gtk.ListStore(str)
		self.authorCombo.set_model(self.authorList)
		cell = gtk.CellRendererText()
		self.authorCombo.pack_start(cell, True)
		self.authorCombo.add_attribute(cell, 'text',0)
		self.authorList.append(['Todos'])
		self.authorCombo.set_active(0)

		#Signals
		callbacks = {
			'on_novo1_activate' : self.on_novo1_activate,
			'on_abrir1_activate' : self.on_abrir1_activate,
			'on_salvar1_activate' : self.on_salvar1_activate,
			'on_salvar_como1_activate' : self.on_salvar_como1_activate,
			'on_sair1_activate' : self.on_sair1_activate,
			'on_recortar1_activate' : self.on_recortar1_activate,
			'on_copiar1_activate' : self.on_copiar1_activate,
			'on_colar1_activate' : self.on_colar1_activate,
			'on_excluir1_activate' : self.on_excluir1_activate,
			'on_about1_activate' : self.on_about1_activate,
			'on_addQuoteButton_clicked' : self.on_addQuoteButton_clicked,
			'on_removeQuoteButton_clicked' : self.on_removeQuoteButton_clicked,
			'on_autorCombo_changed' : self.on_autorCombo_changed,
			'on_hasTextEntry_changed' : self.on_hasTextEntry_changed,
			'on_hasTagsEntry_changed' : self.on_hasTagsEntry_changed,
			'on_cancelbutton1_clicked' : self.on_cancelbutton1_clicked,
			'on_okbutton1_clicked' : self.on_okbutton1_clicked
		}
		self.widgetTree.signal_autoconnect(callbacks)

		self.window.show_all()

	## Callbacks ------------------------------
	def on_novo1_activate(self, widget):
		print 'on_novo1_activate'

	def on_abrir1_activate(self, widget):
		print 'on_abrir1_activate'

	def on_salvar1_activate(self, widget):
		print 'on_salvar1_activate'

	def on_salvar_como1_activate(self, widget):
		print 'on_salvar_como1_activate'

	def on_sair1_activate(self, widget):
		gtk.main_quit();

	def on_recortar1_activate(self, widget):
		print 'on_recortar1_activate'

	def on_copiar1_activate(self, widget):
		print 'on_copiar1_activate'

	def on_colar1_activate(self, widget):
		print 'on_colar1_activate'

	def on_excluir1_activate(self, widget):
		print 'on_excluir1_activate'

	def on_about1_activate(self, widget):
		self.about.show()

	def on_autorCombo_changed(self, widget):
		#self.quoteFilter.set_visible_func(self.quotesVisible, \
		#		data=self.authorCombo.get_active_text())
		self.quoteFilter.refilter()

	def on_hasTextEntry_changed(self, widget):
		print 'on_hasTextEntry_changed'

	def on_hasTagsEntry_changed(self, widget):
		print 'on_hasTagsEntry_changed'

	def on_addQuoteButton_clicked(self, widget):
		self.quote.set_text('')
		self.author.set_text('')
		self.origin.set_text('')
		self.tags.set_text('')
		self.addquote.run()

	def on_removeQuoteButton_clicked(self, widget):
		print 'on_removeQuoteButton_clicked'

	## AddQuoteDialog -------------------------

	def on_cancelbutton1_clicked(self, widget):
		self.addquote.hide()
		
	def on_okbutton1_clicked(self, widget):
		qs, qe = self.quote.get_bounds()
		ts, te = self.tags.get_bounds()
		self.quoteStore.append((self.quote.get_text(qs, qe), \
				self.author.get_text(), self.origin.get_text(), \
				self.tags.get_text(ts, te)))
		self.authorList.append([self.author.get_text()])
		self.addquote.hide()

	## TreeView functions ---------------------
	def quotesVisible(self, model, iter, user_data):
		author = self.authorCombo.get_active_text()
		if author == 'Todos':
			return True
		else:
			return author == model.get_value(iter, 1)

if __name__ == "__main__":
	app = appWindow()
	gtk.main()
